package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.system.domain.SysChicken;
import com.ruoyi.system.service.ISysChickenService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;

import javax.servlet.http.HttpSession;

/**
 * 土鸡信息操作处理
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/chicken_manager/chicken")
public class SysChickenController extends BaseController
{
    private String prefix = "/chicken_manager/chicken";

    @Autowired
    private ISysChickenService chickenService;

    @RequiresPermissions("chicken_manager:chicken:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/chicken";
    }

    @RequiresPermissions("chicken_manager:chicken:list1")
    @PostMapping("/list1")
    @ResponseBody
    public TableDataInfo list(SysChicken chicken)
    {
        startPage();
        List<SysChicken> list = chickenService.selectPostList(chicken);
        return getDataTable(list);
    }

    @Log(title = "土鸡管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("chicken_manager:chicken:export1")
    @PostMapping("/export1")
    @ResponseBody
    public AjaxResult export(SysChicken chicken)
    {
        List<SysChicken> list = chickenService.selectPostList(chicken);
        ExcelUtil<SysChicken> util = new ExcelUtil<SysChicken>(SysChicken.class);
        return util.exportExcel(list, "土鸡数据");
    }

    //领养功能
    @RequiresPermissions("chicken_manager:chicken:adopt")
    @RequestMapping("/adopt")
    @ResponseBody
    public AjaxResult adopt(String ids, HttpSession session)
    {
        System.out.println("领养：》》》》》》》》》》》"+ids);
        SysUser sysUser = ShiroUtils.getSysUser();
        Long[] longs = Convert.toLongArray(ids);
        for (Long aLong : longs) {
            SysChicken chicken = chickenService.selectPostById(aLong);
            //判断是否被领养如果被领养不能删除
            if (chicken.getStatus().equals("0")){
                throw new BusinessException(String.format("%1$s已被领养,不能重新认领", chicken.getChickenName()));
            } else {
                chicken.setChickenID(aLong);
                chicken.setAdoptID(sysUser.getLoginName());
                chicken.setStatus("0");
                chickenService.updatePost(chicken);
            }

        }
        return toAjax(1);
    }

    //回收功能
    @RequiresPermissions("chicken_manager:chicken:recycle")
    @RequestMapping("/recycle")
    @ResponseBody
    public AjaxResult recycle(String ids, HttpSession session)
    {
        System.out.println("回收：》》》》》》》》》》》"+ids);
        SysUser sysUser = ShiroUtils.getSysUser();
        Long[] longs = Convert.toLongArray(ids);
        for (Long aLong : longs) {
            SysChicken chicken = chickenService.selectPostById(aLong);
            chicken.setChickenID(aLong);
            chicken.setAdoptID(" ");
            chicken.setStatus("2");
            chickenService.updatePost(chicken);
        }
        return toAjax(1);
    }

    @RequiresPermissions("chicken_manager:chicken:remove1")
    @Log(title = "土鸡管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove1")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        System.out.println("删除：》》》》》》》》》》》"+ids);
        try
        {
            return toAjax(chickenService.deletePostByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 新增土鸡
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存土鸡
     */
    @RequiresPermissions("chicken_manager:chicken:add")
    @Log(title = "土鸡管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysChicken chicken)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(chickenService.checkPostNameUnique(chicken)))
        {
            return error("新增土鸡'" + chicken.getChickenName() + "'失败，土鸡名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(chickenService.checkPostCodeUnique(chicken)))
        {
            return error("新增土鸡'" + chicken.getChickenName() + "'失败，土鸡编码已存在");
        }
        chicken.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(chickenService.insertPost(chicken));
    }

    /**
     * 修改土鸡
     */
    @GetMapping("/edit/{chickenId}")
    public String edit(@PathVariable("chickenId") Long chickenId, ModelMap mmap)
    {
        mmap.put("chicken", chickenService.selectPostById(chickenId));
        return prefix + "/edit";
    }

    /**
     * 修改保存土鸡
     */
    @RequiresPermissions("chicken_manager:chicken:edit")
    @Log(title = "土鸡管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysChicken chicken)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(chickenService.checkPostNameUnique(chicken)))
        {
            return error("修改土鸡'" + chicken.getChickenName() + "'失败，土鸡名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(chickenService.checkPostCodeUnique(chicken)))
        {
            return error("修改土鸡'" + chicken.getChickenName() + "'失败，土鸡编码已存在");
        }
        chicken.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(chickenService.updatePost(chicken));
    }

    /**
     * 校验土鸡名称
     */
    @PostMapping("/checkPostNameUnique")
    @ResponseBody
    public String checkPostNameUnique(SysChicken chicken)
    {
        return chickenService.checkPostNameUnique(chicken);
    }

    /**
     * 校验土鸡编码
     */
    @PostMapping("/checkPostCodeUnique")
    @ResponseBody
    public String checkPostCodeUnique(SysChicken chicken)
    {
        return chickenService.checkPostCodeUnique(chicken);
    }
}
